﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jmail;

namespace 视频设备
{
    public partial class 找回密码 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var checkCode = TextBox2.Text.Trim();
            生成验证码.YanZhengResult yanResult = 生成验证码.CheckCodeSame(checkCode);
            switch (yanResult)
            {
                case 生成验证码.YanZhengResult.Fail:
                    Response.Write("<Script Language='JavaScript'>window.alert('校验码错误！请重新输入！');window.close();</script>");
                    break;
                case 生成验证码.YanZhengResult.Success:
                    CheckUser();                   
                    break;
                case 生成验证码.YanZhengResult.CodeNull:
                    Response.Write("<Script Language='JavaScript'>window.alert('请获取验证码！')</script>");
                    break;
            }                    
        }

        //返回上一页
        protected void Button2_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //检验验证码           
            var checkCode = TextBox3.Text.Trim();
            生成验证码.YanZhengResult yanResult = 生成验证码.CheckCodeSame(checkCode);
            switch (yanResult)
            {
                case 生成验证码.YanZhengResult.Fail:
                    Response.Write("<Script Language='JavaScript'>window.alert('校验码错误！请重新输入！');window.close();</script>");
                    break;
                case 生成验证码.YanZhengResult.Success:
                    CheckQueAn();
                    break;
                case 生成验证码.YanZhengResult.CodeNull:
                    Response.Write("<Script Language='JavaScript'>window.alert('请获取验证码！')</script>");
                    break;
            }                        
        }

        ////修改密码
        //void altermima()
        //{
        //    Model1Container db = new Model1Container();
        //    观众账号 cmn = db.观众账号集.FirstOrDefault(cc => cc.用户名 == TextBox1.Text);
        //    cmn.密码 = TextBox5.Text;
        //    观众账号 update = db.观众账号集.FirstOrDefault(cc => cc.用户名 == TextBox1.Text);
        //    db.SaveChanges();

        //}

       //检查用户是否存在
        void CheckUser()
        {           
            using (Model1Container db = new Model1Container())
            {
                观众账号 sh = new 观众账号();
                var name = TextBox1.Text.Trim();                
                List<观众账号> sheb = (from u in db.观众账号集 where u.用户名 == name  select u).ToList();
                if (sheb.Count() > 0)
                {
                    sh = db.观众账号集.FirstOrDefault(cc => cc.用户名 == TextBox1.Text);
                    Label1.Text = sh.问题;                    
                    Image2.ImageUrl = "../WebForm1.aspx";
                    MultiView1.ActiveViewIndex = 1;
                }
                else
                {
                    Response.Write("<script>alert('抱歉!该用户不存在!')</script>");
                }
            }
        }
       
        //查找相应用户的密码
        string   CheckMima (string name)
        {
            using (Model1Container db = new Model1Container())
            {
                观众账号 sh = new 观众账号();
                 name = TextBox1.Text.Trim();
                List<观众账号> sheb = (from u in db.观众账号集 where u.用户名 == name select u).ToList();
                sh = db.观众账号集.FirstOrDefault(cc => cc.用户名 == TextBox1.Text);
                var  mima = sh.密码;
                return mima;
            }
        }



        //查询问题与答案是否相符合
        void CheckQueAn()
        {
            观众账号 sh = new 观众账号();
            var name = TextBox1.Text;
            var wenti = Label1.Text;
            var daan = TextBox4.Text;
            观众账号集.AuthResult findResult = 观众账号集.queryUserQueAnser(name, wenti, daan);
            switch (findResult)
            {
                case 观众账号集.AuthResult.Sucess:
                    sendEmail(TextBox1.Text.Trim(), TextBox7.Text.Trim());
                    MultiView1.ActiveViewIndex = 2;
                    break;
                case 观众账号集.AuthResult.DoesnotMatch:
                    Response.Write("<script>alert('抱歉!回答错误!')</script>");
                    break;
            }     
        }

        //发送邮件
        public void sendEmail(string name, string email)
        {
            var User = TextBox1.Text.Trim();
            var Mima = CheckMima (User);
            try
            {
                jmail.Message jmessage = new jmail.Message();
                jmessage.Charset = "GB2312";
                jmessage.ContentType = "text/plain";
                jmessage.From = "13821038913@163.com";
                //发信地址
                jmessage.MailServerUserName = "13821038913";
                jmessage.MailServerPassWord = "Nanwang2008";
                jmessage.FromName = "亚楠";
                jmessage.ReplyTo = "13821038913@163.com";
                jmessage.Subject = "找回密码";
                //内容
                string strbody = User+"您好!您的密码为:"+Mima;
                jmessage.Body = strbody;
                // 邮件标题
                jmessage.AddRecipient(email, "", "");
                jmessage.Send("smtp.163.com",false);                
                jmessage.Close();
            }
            catch (Exception err)
            {
                Response.Write(err);
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }
    }
}